<?php
//公共类
namespace app\api\common;

//TP门面模块
use think\Facade;
//TPResponse模块
use think\Response;
//TPCache模块
use think\facade\Cache;
//TP请求模块
use think\facade\Request;

class Common extends Facade
{
    //COOKIE获取当前用户uid
    protected static function getUid()
    {
        //整理数据
        $uuid = Request::param('UUID');
        //查询缓存的uid
        return Cache::get($uuid);
    }

    //API格式输出方法
    protected static function create($data, string $msg = '', int $code = 200, string $type = 'json') : Response
    {
        $result =[
            //状态码
            'ec' => $code,
            //消息
            'msg' => $msg,
            //数据
            'data' => $data
        ];

        //返回API接口
        return Response::create($result, $type);
    }
    //生成UUID
    protected static function get_uuid()
    {
        $charid = md5(uniqid(mt_rand(), true));
        $hyphen = chr(45);// "-"
        $uuid = //chr(123) "{"
                substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12);
                //.chr(125); "}"
        return $uuid;
    }
}
